package com.zliblike_platform.controller;

import com.zliblike_platform.dto.DownloadHistoryQueryDTO;
import com.zliblike_platform.service.DownloadHistoryService;
import com.zliblike_platform.vo.DownloadHistoryVO;
import com.zliblike_platform.vo.PageResult;
import com.zliblike_platform.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 下载历史控制器
 */
@RestController
@RequestMapping("/api/book/downloads")
public class DownloadHistoryController {

    @Autowired
    private DownloadHistoryService downloadHistoryService;
    
    /**
     * 获取用户下载历史
     */
    @GetMapping
    public ResponseResult<PageResult<DownloadHistoryVO>> getUserDownloadHistory(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 构造查询参数
        DownloadHistoryQueryDTO queryDTO = new DownloadHistoryQueryDTO();
        queryDTO.setUserId(userId);
        queryDTO.setStartDate(startDate);
        queryDTO.setEndDate(endDate);
        queryDTO.setPage(page);
        queryDTO.setSize(size);
        
        // 查询下载历史
        PageResult<DownloadHistoryVO> result = downloadHistoryService.getUserDownloadHistory(queryDTO);
        
        return ResponseResult.success(result);
    }
    
    /**
     * 删除下载历史记录
     */
    @DeleteMapping("/{downloadId}")
    public ResponseResult<Void> deleteDownloadHistory(@PathVariable Long downloadId) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 删除记录
        boolean success = downloadHistoryService.deleteDownloadHistory(downloadId, userId);
        
        if (success) {
            return ResponseResult.success(null);
        } else {
            return ResponseResult.fail(4002, "记录不存在或无权删除");
        }
    }
    
    /**
     * 批量删除下载历史记录
     */
    @DeleteMapping("/batch")
    public ResponseResult<Integer> batchDeleteDownloadHistory(@RequestBody List<Long> downloadIds) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 批量删除记录
        int count = downloadHistoryService.batchDeleteDownloadHistory(downloadIds, userId);
        
        return ResponseResult.success("成功删除" + count + "条记录", count);
    }
} 